package com.leyou.sms;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.leyou.common.constants.MQConstants;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.utils.JsonUtils;
import com.leyou.config.SmsConfiguration;
import com.leyou.constant.SmsConstants;
import com.leyou.constant.SmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * User: ycs
 * Date: 2020/8/28
 * Time: 15:35
 *
 * @author: ycs
 * Description: 注册短信监听类
 */
@Slf4j
@Component
public class SmsListen {
    @Autowired
    private SmsProperties smsProperties;
    @Autowired
    private IAcsClient client;

    public void sendSms(String phone, String code) {
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(smsProperties.getDomain());
        request.setSysVersion(smsProperties.getVersion());
        request.setSysAction(smsProperties.getAction());
        request.putQueryParameter(SmsConstants.SMS_PARAM_REGION_ID, smsProperties.getRegionID());
        request.putQueryParameter(SmsConstants.SMS_PARAM_KEY_PHONE, phone);
        request.putQueryParameter(SmsConstants.SMS_PARAM_KEY_SIGN_NAME, smsProperties.getSignName());
        request.putQueryParameter(SmsConstants.SMS_PARAM_KEY_TEMPLATE_CODE, smsProperties.getVerifyCodeTemplate());
        request.putQueryParameter(SmsConstants.SMS_PARAM_KEY_TEMPLATE_PARAM, "{\"" + smsProperties.getCode() + "\":\"" + code + "\"}");
        try {
            /*发送消息*/
            CommonResponse response = client.getCommonResponse(request);
            //获取放回的消息
            Map<String, String> respData = JsonUtils.toMap(response.getData(), String.class, String.class);

            if (StringUtils.equals(respData.get(SmsConstants.SMS_RESPONSE_KEY_CODE), SmsConstants.OK)) {
                log.info("[短信发送] 成功!!!!");
                return;
            }
            log.error("[短信发送] 失败!! 失败原因:{}", respData.get(SmsConstants.SMS_RESPONSE_KEY_MESSAGE));
        } catch (ServerException e) {
            e.printStackTrace();
            log.error("[短信发送] 失败!! 失败原因:{}", e.getMessage());
            throw new LyException(ExceptionEnum.SEND_MESSAGE_ERROR);
        } catch (ClientException e) {
            e.printStackTrace();
            log.error("[短信发送] 失败!! 失败原因:{}", e.getMessage());
            throw new LyException(ExceptionEnum.SEND_MESSAGE_ERROR);
        }

    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MQConstants.Queue.SMS_VERIFY_CODE_QUEUE, durable = "true"),
            exchange = @Exchange(value = MQConstants.Exchange.SMS_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = MQConstants.RoutingKey.VERIFY_CODE_KEY
    ))
    public void sendCheckCodeMsg(Map<String, Object> map) {
        String phone = (String) map.get("phone");
        String code = (String) map.get("code");
        sendSms(phone, code);
    }
}
